package com.tanhua.recommend.listener;

import com.tanhua.domain.mongo.PublishScore;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
@RocketMQMessageListener(topic = "tanhua-quanzi", consumerGroup = "tanhua-quanzi-consumer")
public class MovementMsgListener implements RocketMQListener<Map> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(Map message) {
        log.info("【MQ消息】接收用户对动态操作信息, topic:{}, message:{}", "tanhua-quanzi", message);
        Long userId = Long.parseLong(message.get("userId").toString());
        Long pid = Long.parseLong(message.get("publishId").toString());
        Integer type = (Integer) message.get("type");

        //将消息信息保存到score表中
        PublishScore publishScore = new PublishScore();
        publishScore.setUserId(userId);
        publishScore.setPublishId(pid);
        publishScore.setDate(System.currentTimeMillis());

        //根据type保存分值
        switch (type) {
            case 1:
                publishScore.setScore(20D);
                break;
            case 2:
                //浏览动态 +1
                publishScore.setScore(1D);
                break;
            case 3:
                //点赞动态 +5
                publishScore.setScore(5D);
                break;
            case 4:
                //喜欢动态 +8
                publishScore.setScore(8D);
                break;
            case 5:
                //文字评论 +10
                publishScore.setScore(10D);
                break;
            case 6:
                //取消点赞 -5
                publishScore.setScore(-5D);
                break;
            case 7:
                //取消喜欢 -8
                publishScore.setScore(-8D);
                break;
            default:
        }

        mongoTemplate.insert(publishScore);
    }
}
